<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="座位管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="660px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
            <el-form-item label="座位名称" prop="seatName">
                <el-input v-model="formData.seatName"/>
            </el-form-item>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="第几排" prop="row">
                        <el-input-number v-model="formData.row"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="第几列" prop="col">
                        <el-input-number v-model="formData.col"/>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="12">
                    <el-form-item label="司机位" prop="isDriverSeat">
                        <el-switch v-model="formData.isDriverSeat" inline-prompt active-text="是" inactive-text="否"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="靠窗" prop="isNearWindow">
                        <el-switch v-model="formData.isNearWindow" inline-prompt active-text="是" inactive-text="否"/>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="12">
                    <el-form-item label="安全门前排" prop="isSafetyDoorFront">
                        <el-switch v-model="formData.isSafetyDoorFront" inline-prompt active-text="是"
                                   inactive-text="否"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="安全门后排" prop="isSafetyDoorBehind">
                        <el-switch
                            v-model="formData.isSafetyDoorBehind"
                            inline-prompt
                            active-text="是"
                            inactive-text="否"
                        />
                    </el-form-item>
                </el-col>
            </el-row>

            <el-form-item label="售票类型" prop="saleTicketType">
                <el-radio-group v-model="formData.saleTicketType">
                    <el-radio v-for="item in Dict.enums.saleTicketType" :label="item.code">
                        {{ item.name }}
                    </el-radio>
                </el-radio-group>
            </el-form-item>

            <el-form-item label="座位状态" prop="status">
                <el-radio-group v-model="formData.status">
                    <el-radio v-for="item in Dict.enums.seatStatus" :label="item.code">{{ item.name }}</el-radio>
                </el-radio-group>
            </el-form-item>

            <el-form-item label="备注" prop="remark">
                <el-input v-model="formData.remark"/>
            </el-form-item>

            <el-form-item label="排序" prop="sort">
                <el-input-number v-model="formData.sort"/>
            </el-form-item>

            <el-form-item>
                <el-popconfirm :title="'确定保存吗？'" @onConfirm="submitForm()">
                    <el-button slot="reference" type="primary">保存</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {VehicleTypeSeatApi} from '@/api/user/VehicleTypeSeatApi'
import {Common} from '@/utils/common'

export default {
    name: 'VehicleTypeSeatUpdate',
    components: {},
    data() {
        return {
            formData: {
                seatName: '',
                seatNo: 0,
                row: 0,
                col: 0,
                isDriverSeat: false,
                isNearWindow: false,
                saleTicketType: '1',
                isSafetyDoorFront: false,
                isSafetyDoorBehind: false,
                remark: '',
                status: '1',
                sort: 0,

                id: 0,
                vehicleTypeId: 0
            },
            rules: {
                seatName: [
                    {required: true, message: '请输入座位名称', trigger: 'blur'},
                    {min: 1, max: 60, message: '长度在 1 到 60 个字符', trigger: 'blur'}
                ]
            },
            dialogVisible: false,
        }
    },
    methods: {
        // 显示组件
        showUpdate(data) {
            this.dialogVisible = true
            if (data.id > 0) {
                this.$nextTick(() => {
                    this.formData = Common.assign(this.formData, data)
                    this.formData.saleTicketType = this.formData.saleTicketType.toString()
                    this.formData.status = this.formData.status.toString()
                })
            } else {
                this.formData.id = 0
                this.formData.vehicleTypeId = data.vehicleTypeId
                if (this.$refs['formRef'] !== undefined) {
                    this.$refs['formRef'].resetFields()
                }
            }
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    if (that.formData.id > 0) {
                        VehicleTypeSeatApi.update(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    } else {
                        VehicleTypeSeatApi.insert(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    }
                }
            })
        }
    }
}
</script>
